var userCols = [[
    {type:'numbers', title:'序号'}
    ,{field:'userName', title:'用户名'}
    ,{field:'email', title:'邮箱地址'}
    ,{field:'phone', title:'联系电话'}
    ,{field:'createTime', title:'添加时间',
        templet: function (res) {
            return  formatDate(res.createTime);
        }
    }
    ,{field:'status', title:'是否启用', templet: '#switchTpl'}
    ,{title:'操作', toolbar: '#authTool', width:200}
]]
var departmentCols = [[
    {type:'numbers', title:'序号'}
    ,{field:'departmentName', title:'部门名称'}
    ,{field:'departmentCode', title:'部门编码'}
    ,{field:'departmentType', title:'部门类型',templet:function(data){
            if (data.departmentType == 1){
                return '部门'
            }else if(data.departmentType == 2){
                return '岗位'
            }else{
                return '其他'
            }
        }}
    ,{field:'sort', title:'排序值'}
    ,{field:'createTime', title:'添加时间',templet:function(data){
            return  formatDate(data.createTime);
        }}
    ,{field:'status', title:'是否启用', templet: '#switchTpl'}
    ,{title:'操作', toolbar: '#authTool', width:200}
]]
var tab_type = '1'
var orderBy = 'create_time'

var main = {

    init:function() {
        this.initTable();
        this.tabBind();
    },
    initTable:function(){
        var _this=this;
        reloadTable(tab_type);

    },
    tabBind:function() {
        layui.use(['form'], function () {
            var form = layui.form;

            form.on('submit(formDemo)', function(data){
                reloadTable(tab_type);
                return false;
            });

            $("[type='reset']").click(function () {
                $(this).parents(".layui-form").find("input").val("");
                $(this).prev().click();
            });

            //排序方式
            form.on('select(orderBy)',function(data){
                orderBy = data.value
                reloadTable(tab_type);
                return false;
            })

            
        });
    }
}




main.init();



/**
 * 加载表格数据
 */
function reloadTable(type){
    layui.use(['table','element'], function() {
        var table = layui.table;
        var  orderBy  = $("#orderBy").val();
        var element = layui.element;
        getList(type,orderBy)
        element.on('tab(tab-all)', function (data) {
            tab_type = $(this).attr('data-type')
            var  orderBy  = $("#orderBy").val();
            getList(tab_type,orderBy)
        })

        function getList(type,orderBy){
            var keyword = $("#keyword").val()
            var position = 'userList'
            var cols = []
            var url = "/user/getSysUserList?"+"&orderBy="+orderBy+"&keyword="+keyword
            var id = "userListTable"
            switch (type) {
                case '1': position = 'userList';cols=userCols; url="/user/getSysUserList?"+"&orderBy="+orderBy+"&keyword="+keyword;break;
                case '2': position = 'departmentList';cols=departmentCols;
                url="/department/getDepartList?departmentType=1&orderBy="+orderBy+"&keyword="+keyword;
                    id = "departmentListTable";break;
                case '3': position = 'positionList';cols=departmentCols;
                    url="/department/getDepartList?departmentType=2&orderBy="+orderBy+"&keyword="+keyword;
                    id = "positionListTable";break;
                default: position = 'userList';
            }
            var tableObj = table.render({
                elem: '#'+position
                ,url:url
                ,title: '数据表'
                ,request:{
                    pageName: 'currentPage',
                    limitName: 'size'
                }
                ,cols: cols
                ,page: true
                ,id : id
            });

            table.on('tool(userList)', function(obj){
                var data = obj.data;
                if(obj.event === 'changeAuth') {
                    localStorage.partyType = "user";
                    localStorage.partyId = data.id;
                    parent.$t.goToPage(this, "/roleAuth/manage");
                }
            });

        }






    });





}